﻿package  {
    import flash.filters.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Scrollbar extends MovieClip {

        public var _ScrollIcon:MovieClip;//instance name
        public var _DownBtn:SimpleButton;//instance name
        public var _ScrollBtn:MovieClip;//instance name
        public var _UpBtn:SimpleButton;//instance name
        public var _ListBack:MovieClip;//instance name
        private var _filterColor:ColorMatrixFilter;
        private var _filters:Array;
        private var _target:MovieClip;
        private var _targetBool:Boolean = false;
        private var _targetY:Number;
        private var _firstBool:Boolean = false;
        private var _isTargetY:Boolean = false;
        private var _showWidth:Number = 0;
        private var _widthBool:Boolean = false;
        private var _mainHeight:Number = 0;
        private var _isMainHeight:Boolean = false;
        private var _isShowBottom:Boolean = false;
        private var _noRefult:Boolean = false;
        private var _showHeight:Number = 0;
        private var _heightBool:Boolean = false;
        private var _moveNum:Number = 0;
        private var _speed:int = 5;
        private var _scrollbarX:int = 0;
        private var _scrollbarXBool:Boolean = false;
        private var _scrollbarY:int = 0;
        private var _scrollbarYBool:Boolean = false;
        private var _maskX:int = 0;
        private var _maskXBool:Boolean = false;
        private var _maskY:int = 0;
        private var _maskYBool:Boolean = false;
        private var _appointLive:Number;
        private var _visibleScrollbar:Boolean = true;
        private var _rect:Sprite;
        private var _rectBool:Boolean = false;
        private var _wheel:Sprite;
        private var _wheelBool:Boolean = false;
        private var _showBottomFirst:Boolean = false;
        private var _loadThisBool:Boolean = false;
        private var ratio:Number = 0;
        private var _move:Number = 0;
        private var _movePoint:Number = 0;
        private var _isMove:Boolean = false;
        private var _isLostUp:Boolean = false;
        private var _isLostDown:Boolean = false;

        public function Scrollbar(){
            this._filterColor = new ColorMatrixFilter([0.2, 0.2, 0.2, 0, 0, 0.2, 0.2, 0.2, 0, 0, 0.2, 0.2, 0.2, 0, 0, 0, 0, 0, 1, 1]);
            this._filters = new Array();
            super();
            trace("滚动条版本0.2");
            this._ScrollBtn.y = 0;
            this._ScrollIcon.mouseEnabled = false;
            this._ListBack.alpha = 0.54;
            this._filters.push(this._filterColor);
        }
        public function set target(_arg1:MovieClip):void{
            if (this._target != _arg1){
                if (this._target != null){
                    this.clear();
                };
            };
            if (_arg1 == null){
                this.scrollUpClear();
            };
            this._target = _arg1;
            if ((((this._target == null)) && (this._firstBool))){
                this.close();
                return;
            };
            if (this._target == null){
                return;
            };
            if (this._isMainHeight == false){
                this._mainHeight = this._target.height;
            };
            if ((((((this._maskYBool == false)) && ((this._isTargetY == false)))) && ((this._firstBool == false)))){
                this._targetY = this._target.y;
                this._firstBool = true;
            };
            this._targetBool = true;
            this.startList();
        }
        public function set targetY(_arg1:Number):void{
            this._targetY = _arg1;
            this._isTargetY = true;
        }
        public function set showWidth(_arg1:Number):void{
            this._showWidth = _arg1;
            this._widthBool = true;
            this.startList();
        }
        public function set mainHeight(_arg1:Number):void{
            this._isMainHeight = true;
            this._mainHeight = _arg1;
        }
        public function get mainHeight():Number{
            return (this._mainHeight);
        }
        public function set showBottom(_arg1:Boolean):void{
            this._isShowBottom = _arg1;
        }
        public function set noRefult(_arg1:Boolean):void{
            this._noRefult = _arg1;
        }
        public function set showHeight(_arg1:Number):void{
            this._showHeight = _arg1;
            this._heightBool = true;
            this.startList();
        }
        public function set moveNum(_arg1:Number):void{
            this._moveNum = _arg1;
        }
        public function set speed(_arg1:int):void{
            this._speed = _arg1;
        }
        public function set liveY(_arg1:int):void{
            this._ScrollBtn.y = _arg1;
            this.scrollMove();
        }
        public function set scrollbarX(_arg1:int):void{
            this._scrollbarX = _arg1;
            this._scrollbarXBool = true;
        }
        public function set scrollbarY(_arg1:int):void{
            this._scrollbarY = _arg1;
            this._scrollbarYBool = true;
        }
        public function set maskX(_arg1:int):void{
            this._maskX = _arg1;
            this._maskXBool = true;
        }
        public function set maskY(_arg1:int):void{
            this._maskY = _arg1;
            this._maskYBool = false;
        }
        public function set appointMc(_arg1:Number):void{
            this._appointLive = (_arg1 - this._showHeight);
            if (((((this._targetBool) && (this._target))) && ((this._appointLive > 0)))){
                this._ScrollBtn.y = (this._appointLive * this._movePoint);
                this.scrollMove();
            };
        }
        public function set visibleScrollbar(_arg1:Boolean):void{
            this._visibleScrollbar = _arg1;
        }
        public function set listBackAlpha(_arg1:Number):void{
            this._ListBack.alpha = _arg1;
        }
        private function createMask():void{
            this.clearMask();
            if (this._maskXBool == false){
                this._maskX = this._target.x;
            };
            if (this._maskYBool == false){
                this._maskY = this._targetY;
            };
            this._rect = new Sprite();
            this._rect.graphics.beginFill(0);
            this._rect.graphics.drawRect((this._maskX - 1), this._maskY, (this._showWidth + 2), this._showHeight);
            this._rect.graphics.endFill();
            this._rect.mouseEnabled = false;
            this._target.parent.addChild(this._rect);
            this._target.mask = this._rect;
            this._rectBool = true;
        }
        private function createWheel():void{
            this.clearWheel();
            this._wheel = new Sprite();
            this._wheel.graphics.beginFill(0);
            this._wheel.graphics.drawRect(0, 0, this._target.width, this._mainHeight);
            this._wheel.graphics.endFill();
            this._target.addChildAt(this._wheel, 0);
            this._target.addEventListener(MouseEvent.MOUSE_WHEEL, this.onScrollWheel);
            this._wheel.alpha = 0;
            this._wheelBool = true;
        }
        private function clearMask():void{
            if (((((this._target) && (this._target.parent))) && (this._rectBool))){
                this._target.mask = null;
                this._target.parent.removeChild(this._rect);
                this._rect = null;
                this._rectBool = false;
            };
        }
        private function clearWheel():void{
            if (((((this._target) && (this._wheelBool))) && (this._wheel.parent))){
                this._wheel.parent.removeChild(this._wheel);
                this._target.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onScrollWheel);
                this._wheel = null;
                this._wheelBool = false;
            };
        }
        private function startList():void{
            if ((((((this._targetBool == false)) || ((this._heightBool == false)))) || ((this._widthBool == false)))){
                return;
            };
            this.clear();
            this.loadScrollbar();
            this.scrollbarLive();
            this.scrollbarSize();
            if (this.ratio >= 1){
                this.renderData();
                this.hideBtn(false);
                this._DownBtn.filters = this._filters;
                this._UpBtn.filters = this._filters;
                this._ListBack.visible = this._visibleScrollbar;
                this._DownBtn.visible = this._visibleScrollbar;
                this._UpBtn.visible = this._visibleScrollbar;
                this._showBottomFirst = true;
            } else {
                this.createMask();
                this.createWheel();
                this.hideBtn(true);
                this._DownBtn.filters = null;
                this._UpBtn.filters = null;
                this._ListBack.visible = true;
                this._DownBtn.visible = true;
                this._UpBtn.visible = true;
                if (this._isShowBottom){
                    if (this._showBottomFirst == true){
                        this._ScrollBtn.y = this._move;
                    };
                    if (this._ScrollBtn.y > (this._move - 10)){
                        this._ScrollBtn.y = this._move;
                    };
                } else {
                    if (((this._noRefult) && ((this._showBottomFirst == false)))){
                    };
                };
                this.scrollMove();
            };
        }
        private function hideBtn(_arg1:Boolean):void{
            this._ScrollBtn.visible = _arg1;
            this._ScrollIcon.visible = _arg1;
        }
        private function loadScrollbar():void{
            if (this._loadThisBool == false){
                this._target.parent.addChild(this);
                this.eventListener();
                this._loadThisBool = true;
            };
        }
        private function removeScrollbar():void{
            if (((this._loadThisBool) && (this.parent))){
                this.parent.removeChild(this);
                this._loadThisBool = false;
            };
        }
        private function scrollbarLive():void{
            if (this._scrollbarXBool == true){
                this.x = this._scrollbarX;
            } else {
                this.x = (this._target.x + this._showWidth);
            };
            if (this._scrollbarYBool == true){
                this.y = this._scrollbarY;
            } else {
                this.y = (this._targetY + this._UpBtn.height);
            };
        }
        private function scrollbarSize():void{
            this._ListBack.height = (this._showHeight - (this._DownBtn.height * 2));
            this._DownBtn.y = this._ListBack.height;
            this.ratio = (this._showHeight / this._mainHeight);
            this._ScrollBtn.height = (this._ListBack.height * this.ratio);
            this._move = (this._ListBack.height - this._ScrollBtn.height);
            if (this._move <= 0){
                this._movePoint = 0;
            } else {
                this._movePoint = (this._move / ((this._mainHeight + this._moveNum) - this._showHeight));
            };
            this.range();
            this._ScrollIcon.y = (this._ScrollBtn.y + (this._ScrollBtn.height / 2));
        }
        private function range():void{
            if (this._ScrollBtn.y > this._move){
                this._ScrollBtn.y = this._move;
            };
            if ((((this._ScrollBtn.y < 0)) || ((this.ratio >= 1)))){
                this._ScrollBtn.y = 0;
            };
            this.scrollMove();
        }
        private function eventListener():void{
            this._UpBtn.addEventListener(MouseEvent.CLICK, this.onUpBtn);
            this._ScrollBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onScrollDown);
            this._DownBtn.addEventListener(MouseEvent.CLICK, this.onDownBtn);
        }
        private function removeListener():void{
            this._UpBtn.removeEventListener(MouseEvent.CLICK, this.onUpBtn);
            this._ScrollBtn.removeEventListener(MouseEvent.MOUSE_DOWN, this.onScrollDown);
            this._DownBtn.removeEventListener(MouseEvent.CLICK, this.onDownBtn);
        }
        private function onScrollWheel(_arg1:MouseEvent):void{
            if (_arg1.delta > 0){
                this.upMove();
            } else {
                this.downMove();
            };
        }
        private function onScrollDown(_arg1:MouseEvent):void{
            this._showBottomFirst = false;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onScrollUp, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onScrollMove, false, 0, true);
            this._ScrollBtn.startDrag(false, new Rectangle(0, 0, 0, this._move));
            this._isMove = true;
        }
        private function onScrollUp(_arg1:MouseEvent):void{
            this.scrollUpClear();
        }
        private function scrollUpClear():void{
            this._ScrollBtn.stopDrag();
            if (stage == null){
                return;
            };
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.onScrollUp);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onScrollMove);
            this._isMove = false;
        }
        private function onScrollMove(_arg1:MouseEvent):void{
            this._isLostDown = false;
            this._isLostUp = false;
            this.scrollMove();
        }
        private function onUpBtn(_arg1:MouseEvent):void{
            this.upMove();
        }
        private function onDownBtn(_arg1:MouseEvent):void{
            this.downMove();
        }
        private function upMove():void{
            this._showBottomFirst = false;
            if ((this._ScrollBtn.y - this._speed) < 0){
                this._ScrollBtn.y = 0;
            } else {
                this._ScrollBtn.y = (this._ScrollBtn.y - this._speed);
            };
            this.scrollMove();
        }
        private function downMove():void{
            this._showBottomFirst = false;
            if ((this._ScrollBtn.y + this._speed) > this._move){
                this._ScrollBtn.y = this._move;
            } else {
                this._ScrollBtn.y = (this._ScrollBtn.y + this._speed);
            };
            this.scrollMove();
        }
        private function scrollMove():void{
            if (stage == null){
                return;
            };
            if (this._movePoint <= 0){
                this._target.y = this._targetY;
            } else {
                this._target.y = (-((this._ScrollBtn.y / this._movePoint)) + this._targetY);
            };
            this._ScrollIcon.y = (this._ScrollBtn.y + (this._ScrollBtn.height / 2));
        }
        private function close():void{
            if (this._isMove){
                this.scrollUpClear();
            };
            this._firstBool = false;
            this._showBottomFirst = true;
            this.clear();
            this.removeListener();
            this.removeScrollbar();
            this._target = null;
        }
        private function clear():void{
            this.clearMask();
            this.clearWheel();
        }
        private function renderData():void{
            this._ScrollBtn.y = 0;
            this.scrollMove();
        }

    }
}//package 
